#include "stdafx.h"
#include "runtime_exceptions.h"
#include "runtime_types.h"
#include "runtime_typecasts.h"
#include "rounding-algorithms.hpp"

String CStr(Double v){
	return std::to_wstring((long double)v);
}

String CStr(Boolean v){
	return (v==true)?L"True":L"False";
}

String CStr(Integer v){
	return std::to_wstring((long double)v);
}

String CStr(Long v){
	return std::to_wstring((long double)v);
}

Double CDbl(String v){
	try {												
		return std::stod(v);	
	}catch (std::invalid_argument ex){												
		throw runtimeException(runtimeException::fbTypeMismatch);												
	}	
}

Integer CInt(String v){										
	return CInt(CDbl(v));
}

Long CLng(String v){							
	return CLng(CDbl(v));	
}

Long CLng(Integer v){							
	return (Long)v;	
}

Long CLng(Boolean v){							
	return (Long)v;	
}

Long CLng(Double v){
	try {												
		return rounding::roundhalfeven<double>(v);	
	}catch (std::invalid_argument ex){												
		throw runtimeException(runtimeException::fbTypeMismatch);												
	}	
}

Integer CInt(Double v){
	try {												
		return rounding::roundhalfeven<double>(v);	
	}catch (std::invalid_argument ex){												
		throw runtimeException(runtimeException::fbTypeMismatch);												
	}	
}

Boolean CBool(Integer v) {
	return (bool)v;
}

Variant CVar(Integer v) {
	return v;
}

Variant CVar(Long v) {
	return v;
}